package com.hb.trans.mvp.ui.fragment


import android.view.View
import com.alibaba.android.arouter.launcher.ARouter
import com.basiclib.base.BaseFragment
import com.basiclib.view.SnackbarUtil
import com.hb.trans.R
import com.hb.trans.bean.ServeChild
import com.hb.trans.bean.ServeList
import com.hb.trans.global.ACTIVITY_HOME_REGISTER
import com.hb.trans.global.ACTIVITY_TRANS_CHECK_LOGIN
import com.hb.trans.global.ACTIVITY_WEB
import com.hb.trans.mvp.contract.ServeContract
import com.hb.trans.mvp.presenter.ServePresenter
import com.hb.trans.mvp.ui.adapter.ServeAdapter
import dagger.android.support.AndroidSupportInjection
import kotlinx.android.synthetic.main.fragment_serve.*
import javax.inject.Inject

class ServeFragment : BaseFragment<ServePresenter>(), ServeContract.View {
    override fun setViewId(): Int {
        return R.layout.fragment_serve
    }

    override fun doOnInvisible() {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun doOnVisible() {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    @Inject
    lateinit var mPresenter: ServePresenter

//    lateinit var rootView: View

    companion object {
        fun newInstance(): ServeFragment {
            val fragment = ServeFragment()
            return fragment
        }
    }

//    override fun initView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
//        rootView = inflater.inflate(R.layout.fragment_serve, container, false)
//        return rootView
//    }

    override fun onFragmentInject() {
        AndroidSupportInjection.inject(this)
    }

    override fun attachView() {
        mPresenter.attachView(this)
    }

    override fun initView() {
    }

    override fun initData() {
    }

    override fun setListener() {
        infoCheckGridView.setOnItemClickListener { parent, view, position, id ->
            val child = infoCheckGridView.adapter.getItem(position) as ServeChild
            when {
                "0c0048566d504336b5f2f29e112f9932" == child.id -> ARouter.getInstance().build(ACTIVITY_TRANS_CHECK_LOGIN).navigation()
                "4727b28599c54573a42081c812cedb9f" == child.id -> ARouter.getInstance().build(ACTIVITY_HOME_REGISTER).navigation()
                else -> {
                    if(child.title.equals("在线学习")){
                        showShortToast("暂未开通", SnackbarUtil.INFO)
                        return@setOnItemClickListener
                    }
                    ARouter.getInstance().build(ACTIVITY_WEB).withString("url", child.redirect_url).navigation()
                }
            }
        }
        outGridView.setOnItemClickListener { _, _, position, _ ->
            val child = outGridView.adapter.getItem(position) as ServeChild
            ARouter.getInstance().build(ACTIVITY_WEB).withString("url", child.redirect_url).navigation()
        }
    }

    override fun setupData(data: ServeList?) {
        data?.let {
            for (serves in it.list) {
                if ("出行服务" == serves.title && serves.childs.size > 0) {
                    tvOutServeTitle.visibility = View.VISIBLE
                    outGridView.apply {
                        val sort = serves.childs.filter { s -> s.status == 1 }.sortedBy { s -> s.sort }.toMutableList()
                        adapter = ServeAdapter(context, sort, R.layout.item_serve)
                    }
                } else if ("信息查询" == serves.title && serves.childs.size > 0) {
                    tvCheckInfoTitle.visibility = View.VISIBLE
                    infoCheckGridView.apply {
                        val sort = serves.childs.filter { s -> s.status == 1 }.sortedBy { s -> s.sort }.toMutableList()
                        adapter = ServeAdapter(context, sort, R.layout.item_serve)
                    }
                }
            }
        }

    }
}
